package org.example.bookshelf_manager.controller;

import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.bookshelf_manager.model.*;
import org.example.bookshelf_manager.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.example.bookshelf_manager.constant.Myconstants;

import java.util.List;

@Slf4j
@RequestMapping("/book")
@RestController
public class BookController {

    @Autowired
    private BookService bookService;


//    @Deprecated   // 废弃的接口
//    @RequestMapping("/getBooklist")
//    public List<BookInfo> getBooklist(){
////        BookService bookService = new BookService();
//        return bookService.getBookList();
//    }

    @RequestMapping("/addBook")
//    添加图书
    public String addBook(BookInfo bookInfo){
        //校验参数
        log.info("添加图书, 接收到参数bookInfo: {}", bookInfo);

        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null){
            return "输入参数不合法";
        }
//        添加图书
        try{
            Integer result = bookService.insertBook(bookInfo);
            if (result > 0){
                return "";
            }
        } catch (Exception e){
            log.error("添加图书异常, e: ",e);
        }
        return "添加失败";
    }

    /*
     * @param :
      * @return java.util.List<org.example.bookshelf_manager.model.BookInfo>
     * @author: ZJX
     * @description:  查询图书列表
     * @date: 2024/8/28 12:12
     */
    @RequestMapping("/getBookListByPage")
    public Result<PageResult<BookInfo>> getBookListByPage(PageRequest pageRequest, HttpSession session){
//        做强制登录的实现 先从session中获取用户信息 如果信息为空 则未登录
        UserInfo logUserInfo = (UserInfo)session.getAttribute(Myconstants.USER_SESSION_KEY);
        if (logUserInfo == null || logUserInfo.getId() <= 0){
             return Result.NoLogin();
        }
        PageResult<BookInfo> bookList = bookService.getBookListByPage(pageRequest);
       return Result.success(bookList);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById(Integer bookId){
        log.info("根据Id查找图书, id" + bookId);
        return bookService.queryBookById(bookId);
    }

    @RequestMapping("/updateBook")
    public String updateBook(BookInfo bookInfo){
        log.info("更新图书, bookInfo: {}", bookInfo);
        try {
            Integer result = bookService.updateBookById(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("更新图书失败 ! e : ",e);
            return "更新图书失败: " + e.getMessage();
        }
    }

//    单个书籍删除
    @RequestMapping("/deleteBook")
    public String deleteBook(BookInfo bookInfo){
//        采用 逻辑删除 只需将 status 设置为 0  数据库里仍保存着信息
        Integer id = bookInfo.getId();
        int result = bookService.deleteBook(id);
        return result > 0 ? "success" : "failure";
    }


//    批量书籍删除
    @RequestMapping("/batchDeleteBook")
    public String batchDeleteBook(@RequestParam List<Integer> ids){
        Integer result = bookService.batchDeleteBook(ids);
        if (result == ids.size()) {
            return "success";
        } else {
            return "failure";
        }
    }
}
